package com.richfit.barcodesystem_cczt.activitis;

import android.os.Bundle;
import android.text.TextUtils;

import com.richfit.barcodesystem_cczt.R;
import com.richfit.barcodesystem_cczt.databinding.ActivityMainBinding;
import com.richfit.barcodesystem_cczt.fragments.BatchCheckOut;
import com.richfit.barcodesystem_cczt.fragments.BatchWarehousing;
import com.richfit.barcodesystem_cczt.fragments.InformationService;
import com.richfit.barcodesystem_cczt.fragments.PositionAdjustment;
import com.richfit.barcodesystem_cczt.fragments.UnpackingAndPacking;
import com.richfit.barcodesystem_cczt.fragments.base.BaseFragment;
import com.richfit.barcodesystem_cczt.scanservice.BaseBarScannerActivity;
import com.richfit.barcodesystem_cczt.utils.constant.CCZT_BizConfig;
import com.richfit.barcodesystem_cczt.utils.constant.Global;
import com.richfit.barcodesystem_cczt.utils.toast.ToastUtil;

/**
 * @author cl
 */
public class Main extends BaseBarScannerActivity {
    private static final String TAG = "Main";
    private ActivityMainBinding binding;
    private String bizType;
    private BaseFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Bundle bundle = getIntent().getExtras();
        //标题
        String title = bundle.getString(Global.EXTRA_CAPTION_KEY);
        binding.icdTop.TxtTitle.setText(title);
        bizType = bundle.getString(Global.EXTRA_BIZ_TYPE_KEY);
        if (TextUtils.isEmpty(bizType)) {
            ToastUtil.showTextShort("业务类型为空!", binding.getRoot());
            return;
        }

        initView();
        initData();
    }

    private void initView() {
        binding.icdTop.IVBack.setOnClickListener(v -> finish());
    }


    private void initData() {
        setFragments(bizType);
    }


    public void setFragments(String bizType) {
        int frame = R.id.frameLayout;
        switch (bizType.toUpperCase()) {
            case CCZT_BizConfig.PLRK:
                /*批量入库*/
                //包含：抬头（1），明细（2）
                fragment = new BatchWarehousing(bizType);
                break;
            case CCZT_BizConfig.ZXRK:
                /*整箱入库*/
                // 包含：台头（1），数据明显（2），数据采集（3）
                break;
            case CCZT_BizConfig.CKQR:
            case CCZT_BizConfig.WZCK:
                //出库确认
                fragment = new BatchCheckOut(bizType);
                break;
            case CCZT_BizConfig.CWTZ:
                //仓位调整
                fragment = new PositionAdjustment(bizType);
                break;
            case CCZT_BizConfig.XHCX:
                //箱号查询
            case CCZT_BizConfig.WZCX:
                //物资条码查询
            case CCZT_BizConfig.LQCX:
                //料签条码查询
                fragment = new InformationService(bizType);
                break;
            case CCZT_BizConfig.CXZX:
                //拆箱装箱
                fragment = new UnpackingAndPacking(bizType);
                break;
            default:
                break;
        }
        if (fragment != null) {
            //加载对应页面
            replace(fragment, frame);
        } else {
            ToastUtil.showTextShort("没有菜单权限");
            finish();
        }
    }

    /**
     * 处理扫描结果，将扫描结果分发到Fragment的中
     *
     * @param type 类别
     * @param list 数据
     */
    @Override
    public void handleBarCodeScanResult(String type, String[] list) {
        if (list == null || list.length <= 0) {
            return;
        }
        if (fragment == null) {
            return;
        }
//        final int fragmentType = mFragmentType;
//        //物料
//        if (list.length > 2 && fragmentType == Global.COLLECT_FRAGMENT_INDEX) {
//            fragment.handleBarCodeScanResult(type, list);
//            //单据
//        } else if (list.length == 1 && fragmentType == Global.HEADER_FRAGMENT_INDEX) {
//            fragment.handleBarCodeScanResult(type, list);
//            //仓位
//        } else if (list.length == 2 && fragmentType == Global.COLLECT_FRAGMENT_INDEX) {
//            fragment.handleBarCodeScanResult(type, list);
//        } else {
//            fragment.handleBarCodeScanResult(type, list);
//        }
        fragment.handleBarCodeScanResult(type, list);
    }
}